// pages/order/createOrder.js

var app = getApp();
let api = require('../../../utils/api.js');
let qqMapWx = require('../../../utils/qqmap-wx-jssdk.min')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fromProvince: '',
    fromCity: '',
    fromAddress: '',
    toProvince: '',
    toCity: '',
    toAddress: '',
    vehicleLen: String,
    vehicleModel: String,
    useVehicleType: String,
    vehicleLoad: String,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      vehicleLen: wx.getStorageSync('VEHICLELEN'),
      vehicleModel: wx.getStorageSync('VEHICLEMODEL'),
      useVehicleType: wx.getStorageSync('VEHICLETYPE'),
      vehicleLoad: wx.getStorageSync('VEHICLELOAD'),
    })
  },
  // 离开出发地
  leaveLocation: function (e) {
    var that = this;
    // 实例化腾讯地图SDK
    var qqmapsdk = new qqMapWx({
      key: 'NT2BZ-Y5Q34-YYCUF-DLQNZ-KW3L5-TCFCC' // 必填
    });
    wx.chooseLocation({
      success: (res) => {
        let latitude = res.latitude;
        let longitude = res.longitude;
        qqmapsdk.reverseGeocoder({
          location: {
            latitude,
            longitude
          },
          success(res) {
            let add = res.result.address_component
            that.setData({
              fromProvince: add.province,
              fromCity: add.city,
              fromAddress:add.district+add.street+add.street_number
            })
          },
          fail(err) {
            console.log(err)
            wx.showToast('获取城市失败')
          }
        })
      }
    });

  },
  // 到达目的地
  arriveLocation(){
    var that = this;
    // 实例化腾讯地图SDK
    var qqmapsdk = new qqMapWx({
      key: 'NT2BZ-Y5Q34-YYCUF-DLQNZ-KW3L5-TCFCC' // 必填
    });
    wx.chooseLocation({
      success: (res) => {
        let latitude = res.latitude;
        let longitude = res.longitude;
        qqmapsdk.reverseGeocoder({
          location: {
            latitude,
            longitude
          },
          success(res) {
            let add = res.result.address_component
            that.setData({
              toProvince: add.province,
              toCity: add.city,
              toAddress:add.district+add.street+add.street_number
            })
          },
          fail(err) {
            console.log(err)
            wx.showToast('获取城市失败')
          }
        })
      }
    });
  },
  // 输入监听
  inputWacth: function (e) {
    let item = e.currentTarget.dataset.model;
    this.setData({
      [item]: e.detail.value
    });
  },
  /**
   * 添加车厂车型
   */
  car: function (e) {
      wx.navigateTo({
        url: 'car',
      })
  },
   /*
   * 选择重量
   */
  carWeight: function (e) {
    wx.navigateTo({
      url: 'carWeight/carWeight',
    })
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      keyboardHeight: 0
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showCarModelStatus: false,
        keyboardHeight: 0
      })
    }.bind(this), 200)
  },
  // 获取键盘高度
  keyBoardHeight: function (e) {
    this.setData({
      keyboardHeight: e.detail.height
    });
  },
  // 保存常用路线
  save: function (e) {
    var that = this;
    var data = that.data;
    if (data.useVehicleType.length == 0 || data.vehicleLen.length == 0 || data.vehicleModel.length == 0 || data.vehicleLoad.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请输入完整信息后再进入下一步',
        success: function (res) {
          if (res.confirm) {
            
          }
        }
      });
      return;
    }

    var param = {
      "fromCity": data.fromCity,
      "fromProvince": data.fromProvince,
      "fromAddress": data.fromAddress,
      "toAddress": data.toAddress,
      "toCity": data.toCity,
      "toProvince": data.toProvince,
      "vehicleType": data.useVehicleType,
      "vehicleLen": data.vehicleLen.toString(),
      "vehicleModel": data.vehicleModel,
      "vehicleLoad": data.vehicleLoad
    }
    var token = wx.getStorageSync('TOKEN');
    api._post('/chauffeur/route/add', param, token).then(res => {
      if (res.code == 200) {
        wx.switchTab({
          url: '../../index/index',
          success: function () {
            var page = getCurrentPages().pop();
            if (page == undefined || page == null) {
              return
            }
            page.onLoad();
          }
        })
      } else if (res.code > 400) {
        wx.navigateTo({
          url: '../../login/login',
        })
      } else {
        wx.showToast({
          title: '网络忙请重试',
          icon: 'none',
        })
      }
    })
  }
})